*****************************************************************************************************
* co-morbidities 
*****************************************************************************************************

* Thyroid
gen  thyroid=0
foreach i of varlist diag_secun diagsec* cid_asso  {
replace  thyroid=1 if substr(`i', 1, 2)=="I0" 
}

* oseao
gen  oceo=0
foreach i of varlist diag_secun diagsec* cid_asso  {
replace  oceo=1 if substr(`i', 1, 1)=="M" 
}

* abnormalities
gen  abnormalities=0
foreach i of varlist diag_secun diagsec* cid_asso  {
replace  abnormalities=1 if substr(`i', 1, 1)=="Q" 
}

* valvular disease 
gen  valvular_disease=0
foreach i of varlist diag_secun diagsec*  {
replace  valvular_disease=1 if substr(`i', 1, 3)=="I34" | substr(`i', 1, 3)=="I35" | substr(`i', 1, 3)=="I36"  ///
| substr(`i', 1, 3)=="I37" | substr(`i', 1, 3)=="I38"   | substr(`i', 1, 3)=="I39" 
} 

*peripheral Vascular Disorder
gen  peri_valvular_disease=0
foreach i of varlist diag_secun diagsec*  {
replace  peri_valvular_disease=1 if substr(`i', 1, 2)=="I7" 
} 

*Neuro Disorder
gen  nervous=0
foreach i of varlist diag_secun diagsec*  {
replace  nervous=1 if substr(`i', 1, 1)=="G" 
} 

* brain
gen  brain=0
foreach i of varlist diag_secun diagsec*  {
replace  brain=1 if substr(`i', 1, 2)=="I6" 
}

* chronic heart 
gen  chronic_heart=0
foreach i of varlist diag_secun diagsec*  {
replace  chronic_heart=1 if substr(`i', 1, 3)=="I05" | substr(`i', 1, 3)=="I06" | substr(`i', 1, 3)=="I07" ///
| substr(`i', 1, 3)=="I08" | substr(`i', 1, 3)=="I09"
}

* chronic isquem
gen  chronic_isquem=0
foreach i of varlist diag_secun diagsec*  {
replace  chronic_isquem=1 if substr(`i', 1, 3)=="I25" 
}

*congestive heart failure
gen  congestive_heart_failure=0
foreach i of varlist diag_secun diagsec*  {
replace  congestive_heart_failure=1 if substr(`i', 1, 3)=="I50" 
}


*chronic lung 
gen  lung=0
foreach i of varlist diag_secun diagsec*  {
replace  lung=1 if substr(`i', 1, 2)=="J4" 
}

* hiv 
gen  hiv=0
foreach i of varlist diag_secun diagsec*  {
replace  hiv=1 if substr(`i', 1, 3)=="B20" | substr(`i', 1, 3)=="B21"  | substr(`i', 1, 3)=="B22" ///
 | substr(`i', 1, 3)=="B23"  | substr(`i', 1, 3)=="B24"
}


* figado
gen  liver=0
foreach i of varlist diag_secun diagsec*  {
replace  liver=1 if substr(`i', 1, 2)=="K7"
}


* Mental
gen  mental=0
foreach i of varlist diag_secun diagsec*  {
replace  mental=1 if substr(`i', 1, 1)=="F"
}


* kidney - crhonic 
gen  kidney=0
foreach i of varlist diag_secun diagsec*  {
replace  kidney=1 if substr(`i', 1, 3)=="N18" 
}

* previous AMI 
gen  previous_AMI=0
foreach i of varlist diag_secun diagsec*  {
replace  previous_AMI=1 if substr(`i', 1, 3)=="I22" 
}

* hipertension 
gen  hipertensao=0
foreach i of varlist diag_secun diagsec*  {
replace  hipertensao=1 if substr(`i', 1, 3)=="I10" | substr(`i', 1, 3)=="I11" ///
| substr(`i', 1, 3)=="I12" | substr(`i', 1, 3)=="I13" | substr(`i', 1, 3)=="I14" ///
| substr(`i', 1, 3)=="I15"
}

* hipertension primaria 
gen  hipertensao_prim=0
foreach i of varlist diag_secun diagsec* cid_asso {
replace  hipertensao_prim=1 if substr(`i', 1, 3)=="I10" 
}

* hipertension secundaria
gen  hipertensao_sec=0
foreach i of varlist diag_secun diagsec* cid_asso {
replace  hipertensao_sec=1 if substr(`i', 1, 3)=="I15" 
}

* hipertension card renal
gen  hipertensao_cardrenal=0
foreach i of varlist diag_secun diagsec* cid_asso {
replace  hipertensao_cardrenal=1 if substr(`i', 1, 3)=="I11" | substr(`i', 1, 3)=="I12" | substr(`i', 1, 3)=="I13" 
}

* Diabetes I
gen  diabetes_I=0
foreach i of varlist diag_secun diagsec* cid_asso {
replace  diabetes_I=1 if substr(`i', 1, 3)=="E10" 
}

* Diabetes II
gen  diabetes_II=0
foreach i of varlist diag_secun diagsec* cid_asso {
replace  diabetes_II=1 if substr(`i', 1, 3)=="E11" 
}

gen diabetes_I_II=diabetes_I==1 | diabetes_II ==1


* Diabetes other
gen  diabetes_other=0
foreach i of varlist diag_secun diagsec* cid_asso {
replace  diabetes_other=1 if substr(`i', 1, 3)=="E12" | substr(`i', 1, 3)=="E13" | substr(`i', 1, 3)=="E14" 
}


* Diabetes
gen  diabetes=0
foreach i of varlist diag_secun diagsec*  {
replace  diabetes=1 if substr(`i', 1, 3)=="E10" | substr(`i', 1, 3)=="E11" | substr(`i', 1, 3)=="E12" | substr(`i', 1, 3)=="E13" ///
| substr(`i', 1, 3)=="E14"
}

* Diabetes sem compli
gen  diabetes_semc=0
foreach i of varlist diag_secun diagsec* cid_asso {
replace  diabetes_semc=1 if substr(`i', 1, 4)=="E109" | substr(`i', 1, 4)=="E119" | substr(`i', 1, 4)=="E129" | substr(`i', 1, 4)=="E139" ///
| substr(`i', 1, 4)=="E149"
}

* Diabetes com compli
gen diabetes_c=diabetes
replace diabetes_c=0 if diabetes_semc ==1

* Obesidade
gen  obesidade=0
foreach i of varlist diag_secun diagsec* cid_asso {
replace  obesidade=1 if substr(`i', 1, 3)=="E65" | substr(`i', 1, 3)=="E66" | substr(`i', 1, 3)=="E67" | substr(`i', 1, 3)=="E68"
}

* metabolic disorders
gen  meta_disorder=0
foreach i of varlist diag_secun diagsec* cid_asso {
replace  meta_disorder=1 if substr(`i', 1, 2)=="E7" | substr(`i', 1, 2)=="E8" | substr(`i', 1, 2)=="E9" 
}

* cancer 
gen  cancer=0
foreach i of varlist diag_secun diagsec*  {
replace  cancer=1 if substr(`i', 1, 1)=="C" | substr(`i', 1, 2)=="D0" | substr(`i', 1, 2)=="D1" | substr(`i', 1, 2)=="D2" | substr(`i', 1, 2)=="D3" ///
| substr(`i', 1, 2)=="D4"
}

